﻿//地区选择
(function ($) {

    //n表单名,v"湖南,长沙,天心区",d默认值:"请选省,请选择市,请选择区/县"
    //加载
    $.fn.District = function (v, n, d, callback, style, getStreetsUrl) {
        n = n || "KADDistrict";
        v = v || "";
        d = (d || "请选择省,请选择市,请选择区/县,请选择街道").split(",");
        $.fn.District.LinkageDefault = d;
        $.fn.District.LinkageLen = d.length; //联动多少层
        style = style || "";
        $.fn.District.GetStreetsUrl = getStreetsUrl || '/Address/GetStreets';

        var data = $.fn.District.Data;
        var id = n.replace(/\./g, "_");
        if ($("#" + id).length == 0) {
            this.append($('<input type="hidden">').attr({
                "name": n,
                "id": id
            }));
            this.append($('<input type="hidden">').attr({
                "name": n + "_code",
                "id": id + "_code"
            }));
        }

        for (var i = 0; i < $.fn.District.LinkageLen; i++) {
            var s = $("<select>")
                .append($("<option>")
                    .html(d[i].replace('{name}', ''))
                    .attr({
                        'value': '',
                        def: d[i]
                    })).attr({
                    "n": id,
                    "id": id + "_" + i,
                    "i": i,
                    "class": style
                });
            this.append(s);
            s.change($.fn.District.Change);

            if (i == 0) {
                $.fn.District.FillData(id, -1);
            }
        }

        $.fn.District.CallBack = callback;
        $.fn.District.SetValue(id, v);
    };
    //选择完成后的事件
    $.fn.District.CallBack = null;
    //select 的 id
    //选择事件
    $.fn.District.Change = function () {
        var o = $(this);
        var index = parseInt(o.attr('i'));
        var name = o.attr('n');
        $.fn.District.FillData(name, index);
        var r = [];
        var rCode = [];

        // 验证结果及描述
        var areaVerifyResult = {
            isSuccess: true,
            msg: '验证通过'
        };
        $('select[n=' + name + ']').each(function (v) {
            r.push($($(this).get(0)).val());
            var thatId = $($(this).get(0)).find("option:selected").attr("id") || "";
            rCode.push(thatId);

            if (areaVerifyResult.isSuccess) {
                areaVerifyResult.isSuccess = !$(this).is(":hidden") && !thatId ? false : true;
                areaVerifyResult.msg = areaVerifyResult.isSuccess ? areaVerifyResult.msg : $.fn.District.LinkageDefault[$(this).attr('i')];
            }
        });
        $('#' + name).val(r == ',' ? "" : r);
        $('#' + name + '_code').val(rCode == ',' ? "" : rCode);
        $.fn.District.CallBack ? $.fn.District.CallBack(r, areaVerifyResult) : null;
    };
    //填充数据
    $.fn.District.FillData = function (name, index) {
        var id = 0;
        var parent = "";
        var isPrev = false;
        if (index > -1) {
            var p = $("#" + name + "_" + index);
            if (p.attr('prev')) {
                isPrev = true;
                var prevP = $("#" + name + "_" + (--index));
                var prevOpt = prevP.get(0);
                var prevO = $(prevOpt[prevOpt.selectedIndex]);
                id = prevO.attr('id');
                parent = prevO.html();
            } else {
                var opt = p.get(0);
                var o = $(opt[opt.selectedIndex]);
                id = o.attr('id');
                parent = o.html();
            }
            index = parseInt(p.attr("i"));
        }
        var o = $("#" + name + "_" + (++index));
        if (o.length) {
            o.get(0).length = 1;
            var def = $(o.get(0)[0]);
            def.html(def.attr('def').replace('{name}', parent));
            var data = $.fn.District.Data[id];
            if (isPrev || typeof (data) === 'undefined') {
                //街道
                data = getStreets(id);
            }
            if (data && data.length) {
                for (var i = 0; i < data.length; i++) {
                    o.append($("<option>").html(data[i][1]).attr('id', data[i][0]));
                }
                if (i == 1) {
                    o.get(0)[i].selected = true;
                }
                o.show();
                if (index + 1 < $.fn.District.LinkageLen) {
                    o.triggerHandler('change');
                }
            } else {
                for (var i = index; i < $.fn.District.LinkageLen; i++) {
                    var x = $("#" + name + "_" + (i));
                    if (x.length > 0) {
                        x.get(0).length = 1
                    }
                    x.hide();
                }
            }
            o.removeAttr('prev');
            // 处理广东东莞等只有二级的特殊数据
            if (data && data.length === 0) {
                o.attr('prev', 1);
                o.triggerHandler('change');
            }
        }
    };
    //选中
    $.fn.District.SetValue = function (name, v) {
        v = v.split(",");
        for (var i = 0; i < $.fn.District.LinkageLen; i++) {
            var o = $("#" + name + "_" + i);
            if (o.length && v[i]) {
                var l = o.get(0);
                for (var j = 0; j < l.length; j++) {
                    if (l[j].text == v[i]) {
                        l[j].selected = true;
                        o.triggerHandler('change');
                        break;
                    }
                }
            }
        }
    };

    $.fn.District.Streets = {};
    //获取街道信息
    function getStreets(code) {
        var streets;
        if (!code) {
            return streets;
        }
        streets = $.fn.District.Streets[code];
        if (streets) {
            return streets;
        }
        $.ajax({
            url: $.fn.District.GetStreetsUrl,
            data: {
                code: code
            },
            type: 'Get',
            cache: true,
            dataType: 'json',
            async: false,
            success: function (data) {
                if (data.Data && data.Data.length > 0) {
                    $.fn.District.Streets[code] = data.Data;
                    streets = data.Data;
                }
            }
        });
        return streets;
    }
})(jQuery);